package com.baizhi.controller;

import com.baizhi.dto.PageDTO;
import com.baizhi.entity.User;
import com.baizhi.entity.Video;
import com.baizhi.service.VideoService;
import com.baizhi.vo.CommonQueryPageVO;
import com.baizhi.vo.CommonVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;

@CrossOrigin
@RestController
@RequestMapping("video")
public class VideoController {

    private static final Logger log = LoggerFactory.getLogger(VideoController.class);
    @Resource
    VideoService videoService;

    @RequestMapping("queryAllPage")
    public CommonQueryPageVO queryAllPage(@RequestBody PageDTO pageDTO){

        log.info("当前页数：{}",pageDTO.getPage());
        log.info("每页展示条数：{}",pageDTO.getPageSize());

        CommonQueryPageVO commonQueryPageVO = videoService.queryAllPage(pageDTO.getPage(), pageDTO.getPageSize());
        return commonQueryPageVO;
    }

    @RequestMapping("update")
    public CommonVO update(@RequestBody Video video){
        log.info("修改视频数据：{}",video);
        try {
            videoService.update(video);
            return CommonVO.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonVO.success("修改失败");
        }
    }

    @RequestMapping("add")
    public CommonVO add(@RequestBody Video video){
        log.info("添加视频数据：{}",video);
        try {
            videoService.add(video);
            return CommonVO.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonVO.success("添加失败");
        }
    }

    @RequestMapping("uploadVideo")
    public HashMap<String, String> uploadVideo(MultipartFile videoFile){
        return videoService.uploadVideoAliyun(videoFile);
    }
}
